package com.tianpengtech.front.mobile.login;

import com.jfinal.aop.Before;
import com.jfinal.aop.Clear;
import com.jfinal.kit.Ret;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.tianpengtech.common.util.IpKit;
import com.tianpengtech.front.mobile.common.intercepter.MobileRequestInterceptor;
import com.tianpengtech.front.mobile.common.intercepter.TokenCheckInterceptor;
import com.tianpengtech.front.mobile.common.util.R;
import com.tianpengtech.front.mobile.common.util.U;
import com.tianpengtech.front.web.common.controller.BaseController;
import com.tianpengtech.front.web.passport.PassportService;

/**
 * Created by ggd on 2017/7/18 0018.
 */
public class MobileLoginController extends BaseController {

    private static MobileLoginService service = new MobileLoginService();

    private static PassportService passportService = new PassportService();
    /**
     * 登录
     */
    @Clear(TokenCheckInterceptor.class)
    @Before(Tx.class)
    public void login(){
        String mobile=getPara("mobile");
        String code= getPara("authCode");
        if (!U.isMobile(mobile)){
            renderJson(R.fail("手机号码格式不正确"));
            return;
        }
        if (!U.isInteger(code)){
            renderJson(R.fail("验证码格式不正确"));
            return;
        }
        String loginIp = IpKit.getRealIp(getRequest());
        R r = null;
        try {
            r = service.login(mobile,Integer.parseInt(code),loginIp,getHeader("source"));
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(R.fail("注册失败"));
            return;
        }
        renderJson(r);
    }

    /**
     * 发送验证码
     */
    @Clear(TokenCheckInterceptor.class)
    public void sendCode(){
        String mobile=getPara("mobile");
        if (!U.isMobile(mobile)){
            renderJson(R.fail("手机号码格式不正确"));
            return;
        }
        String ip = IpKit.getRealIp(getRequest());
        Ret ret=passportService.sendAuthCode(mobile,ip);
        if (ret.isOk()){
            R r=R.ok("发送成功");
            r.p("code",ret.get("code"));
            renderJson(r);
        }else{
            renderJson(R.fail("发送失败"));
        }
    }

    /**
     * 退出登录
     */
    public void logout(){
        String token = getPara("token");
        if (StrKit.isBlank(token)){
            renderJson(R.fail("token不能为空"));
            return;
        }
        R r=service.logout(token);
        renderJson(r);
    }


    /**
     * 邀请注册链接 跳转页面
     */
    @Clear({TokenCheckInterceptor.class,MobileRequestInterceptor.class})
    public void invite(){
        String code=getPara("code");
        setAttr("code",code);
        render("/WEB-INF/view/web/index/invite.html");
    }

    /**
     * 邀请注册 同所有api接口
     */
    @Clear({TokenCheckInterceptor.class})
    public void inviteReg(){
        String code=getPara("code");
        String mobile=getPara("mobile");
        String authCode=getPara("authCode");
        if (StrKit.isBlank(code)){
            renderJson(R.fail("邀请码不能为空"));
            return;
        }
        if (StrKit.isBlank(mobile)){
            renderJson(R.fail("手机号码不能为空"));
            return;
        }
        if (StrKit.isBlank(authCode)){
            renderJson(R.fail("验证码不能为空"));
            return;
        }
        if (!U.isInteger(authCode)){
            renderJson(R.fail("验证码格式不正确"));
            return;
        }
        String regIp = IpKit.getRealIp(getRequest());
        try {
            R r=service.inviteReg(code,mobile,Integer.parseInt(authCode),regIp);
            renderJson(r);
        } catch (Exception e) {
            e.printStackTrace();
            renderJson(R.fail("注册失败"));
            return;
        }
    }
}
